package zpy.servlet.servlet;

import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import zpy.servlet.pojo.AjaxResult;
import zpy.servlet.pojo.Department;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class SelectServlet
 */
@WebServlet("/SelectServlet")
public class SelectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SelectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		List<Department> dList = new ArrayList<>();
		String option = request.getParameter("option");
		if (option == null || "1".equals(option)) {//a，表单提交
			String college = request.getParameter("college"); 
			request.setAttribute("cs", college);
			if (college != null) {
				setDepartMent(dList,college);
			}
			request.setAttribute("departmentList", dList);
			//显示选项
			List<String> selected = new ArrayList<>();
			Map<String, String[]> selectedMap = request.getParameterMap();
			for(Map.Entry<String, String[]> entry : selectedMap.entrySet()){
				String key = entry.getKey();
				String[] values = entry.getValue();
				for(String value:values){
					selected.add(key + "---->>"+value);
				}
			}
			request.setAttribute("selected",selected);
			getServletContext().getRequestDispatcher("/pages/threeChapt/select.jsp")
			.forward(request, response);
		}else{//ajax异步访问
			request.setCharacterEncoding("utf-8");
			String college = request.getParameter("colloge");
			setDepartMent(dList, college);
			AjaxResult result = AjaxResult.success("获取系部成功", dList);
			response.setContentType("text/plain;charset=utf-8");
			Writer writer = response.getWriter();
			Gson gson = new Gson();
			writer.write(gson.toJson(result));
			writer.close();
		}
	}

	private void setDepartMent(List<Department> dList, String college) {
		// TODO Auto-generated method stub
		switch (college){
		case "01":{
			Department department = new Department();
			department.setCode("0101");
			department.setName("古代汉语");
			dList.add(department);
			department = new Department();
			department.setCode("0102");
			department.setName("现代汉语");
			dList.add(department);
			department = new Department();
			department.setCode("0103");
			department.setName("汉语教育");
			dList.add(department);
			department = new Department();
			department.setCode("0104");
			department.setName("大众传媒");
			dList.add(department);
			break;
		}
		case "02":{
			Department department = new Department();
			department.setCode("0201");
			department.setName("计算机科学与应用");
			dList.add(department);
			department = new Department();
			department.setCode("0202");
			department.setName("数字媒体");
			dList.add(department);
			department = new Department();
			department.setCode("0203");
			department.setName("电气工程");
			dList.add(department);
			department = new Department();
			department.setCode("0204");
			department.setName("电子技术");
			dList.add(department);
			department = new Department();
			department.setCode("0205");
			department.setName("信息安全");
			dList.add(department);
			break;
		}
		case "03":{
			Department department = new Department();
			department.setCode("0301");
			department.setName("经典数学");
			dList.add(department);
			department = new Department();
			department.setCode("0302");
			department.setName("现代数学");
			dList.add(department);
			department = new Department();
			department.setCode("0303");
			department.setName("计算科学");
			dList.add(department);
			department = new Department();
			department.setCode("0304");
			department.setName("机械工程");
			dList.add(department);
			break;
		}
		case "04":{
			Department department = new Department();
			department.setCode("0401");
			department.setName("古代化学");
			dList.add(department);
			department = new Department();
			department.setCode("0402");
			department.setName("现代化学");
			dList.add(department);
			department = new Department();
			department.setCode("0403");
			department.setName("化学教育");
			dList.add(department);
			department = new Department();
			department.setCode("0404");
			department.setName("爆炸控制");
			dList.add(department);
			break;
		}
		case "05":{
			Department department = new Department();
			department.setCode("0501");
			department.setName("中医学");
			dList.add(department);
			department = new Department();
			department.setCode("0502");
			department.setName("西医学");
			dList.add(department);
			department = new Department();
			department.setCode("0503");
			department.setName("临床医学");
			dList.add(department);
			department = new Department();
			department.setCode("0504");
			department.setName("护理学");
			dList.add(department);
			break;
		}
		default:
			break;
		}

	}

}
